package services;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * Created by IntelliJ IDEA.
 * User: adminx
 * Date: 14.08.12
 * Time: 11:19
 * To change this template use File | Settings | File Templates.
 */

public class BookServices {

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource ds) {
        dataSource = ds;
    }

    public void createTableBook(){
        jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("CREATE table Book(id int NOT NULL AUTO_INCREMENT PRIMARY KEY ,nameBook varchar(255),nameAuthor varchar(255)," +
                "publishing varchar(255),description varchar(255))");
    }

    public void createBook(String nameBook, String nameAuthor, String publishing, String description) {
        jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("INSERT into Book (nameBook,nameAuthor,publishing,description) VALUES (?,?,?,?)",
                new Object[]{nameBook, nameAuthor, publishing, description});
    }

    public void deleteBook(Integer id){
        jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("delete from Book where id = ?", new Object[] {id});
    }

    public int getBook(Integer id){
        jdbcTemplate = new JdbcTemplate(dataSource);
        int book = jdbcTemplate.update("SELECT * from Book where id =?", new Object[]{id});
        return book;
    }
}
